<?php
/** @noinspection PhpPropertyCanBeReadonlyInspection */
declare(strict_types=1);

namespace Dreamcat\ApiDoc\AddSimpleVariableHelper;

use Dreamcat\ApiDoc\Error\GeneratorError;
use Dreamcat\ApiDoc\Popo\ApiDefinition;
use Dreamcat\ApiDoc\Popo\BodyDefinition;

/**
 * 添加PathVariable
 * @author vijay
 */
class AddPathVariable extends AbstractAddSimpleVariableHelper
{
    /**
     * @param string[] $pathVariables uri中定义的路径变量
     */
    public function __construct(private array $pathVariables)
    {
    }

    /**
     * @inheritDoc
     */
    protected function addDefinition(ApiDefinition $apiDefinition, BodyDefinition $definition): ApiDefinition
    {
        return $apiDefinition->addPathVariableDefinition($definition);
    }

    protected function checkField(string $field): void
    {
        parent::checkField($field);
        if (!isset($this->pathVariables[$field])) {
            throw new GeneratorError("未定义的 {$this->getVariableName()} 变量 {$field}", GeneratorError::PATH_VAR_UNDEFINED);
        }
    }

    /**
     * @inheritDoc
     */
    protected function getVariableName(): string
    {
        return "PathVariable";
    }
}

# end of file
